package com.imddy.gkrpc;

import java.lang.reflect.Method;
import java.util.Arrays;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * 表示服务的一个类
 * @author Administrator
 *
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ServiceDescriptor {
    /** 服务的类名 */
	private String clazz;
	/** 服务的方法名 */
	private String method;
	/** 服务的返回类型 */
	private String returnType;
	/** 服务的传入参数 */
	private String[] parameterTypes;
	
	public static ServiceDescriptor from(Class clazz, Method method) {
		ServiceDescriptor sdp = new ServiceDescriptor();
		sdp.setClazz(clazz.getName());
		sdp.setMethod(method.getName());
		sdp.setReturnType(method.getReturnType().getName());
		
		Class[] parameterClasses = method.getParameterTypes();
		String[] parameterTypes = new String[parameterClasses.length];
		for (int i = 0; i < parameterClasses.length; i++) {
			parameterTypes[i] = parameterClasses[i].getName();
		}
		sdp.setParameterTypes(parameterTypes);
		
		return sdp;
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return toString().hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		ServiceDescriptor that = (ServiceDescriptor) obj;
		return this.toString().equals(that.toString());
		
	}
	
	@Override
	public String toString() {
		return "clazz=" + clazz
				+ ", method=" + method
				+ ",returnType=" + returnType
				+ ",parameterTypes=" + Arrays.toString(parameterTypes);
	}
}
